package com.mixmall.config;


import com.mixmall.entity.MaiProductUser;
import com.mixmall.service.IMaiProductUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Autowired
    CustoUserDetailService custoUserDetailService;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
        return http
                .formLogin(form -> form
                .permitAll()
//                .successHandler(new CustAuthenticationSuccess("http://localhost:8081/#/pages/public/login"))
                )
                .authorizeRequests(authoriza->
                        authoriza
                .mvcMatchers("/mixmall/maiProductCate").hasRole("admin").anyRequest().authenticated()
                )
                        .userDetailsService(custoUserDetailService).build();
    }
}
